package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 等级特权管理控制器
 */
@RestController
@RequestMapping("/admin/privilege")
@Tag(name = "等级特权管理", description = "等级特权管理相关接口")
@Slf4j
public class LevelPrivilegeController {

    @Autowired
    private LevelPrivilegeService levelPrivilegeService;

    /**
     * 添加等级特权
     *
     * @param command 等级特权命令
     * @return 结果
     */
    @PostMapping
    @Operation(summary = "添加等级特权", description = "主要用于等级特权管理-特权管理")
    public Result<Void> addLevelPrivilege(@RequestBody LevelPrivilegeCommand command) {
        levelPrivilegeService.addLevelPrivilege(command);
        return Result.ok();
    }

    /**
     * 修改等级特权
     *
     * @param command 等级特权命令
     * @return 结果
     */
    @PutMapping
    @Operation(summary = "修改等级特权", description = "主要用于等级特权管理-特权管理")
    public Result<Void> updateLevelPrivilege(@RequestBody LevelPrivilegeCommand command) {
        levelPrivilegeService.updateLevelPrivilege(command);
        return Result.ok();
    }

    /**
     * 获取等级特权列表
     *
     * @param request 分页请求
     * @return 等级特权列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取等级特权列表", description = "主要用于等级特权管理-特权管理")
    public Result<PageDTO<LevelPrivilegeDTO>> getLevelPrivilegeList(@ModelAttribute LevelPrivilegePageRequest request) {
        // 记录接收到的查询参数
        log.info("接收到特权查询请求，等级ID: {}, 页码: {}, 数量: {}",
                request.getLevelId(), request.getPageNum(), request.getCount());

        PageDTO<LevelPrivilegeDTO> pageDTO = levelPrivilegeService.getLevelPrivilegeList(request);
        return Result.ok(pageDTO);
    }

    /**
     * 删除指定的等级特权
     *
     * @param id 特权ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除指定的等级特权", description = "主要用于等级特权管理-特权管理")
    public Result<DeleteLevelPrivilegeOperateDTO> deleteLevelPrivilege(@PathVariable Long id) {
        log.info("删除等级特权, id: {}", id);
        DeleteLevelPrivilegeOperateDTO operateDTO = levelPrivilegeService.deleteLevelPrivilege(id);
        return Result.ok(operateDTO);
    }

    /**
     * 批量删除等级特权
     *
     * @param command 批量删除命令
     * @return 结果
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除等级特权", description = "主要用于等级特权管理-特权管理")
    public Result<List<DeleteLevelPrivilegeOperateDTO>> batchDeleteLevelPrivilege(@RequestBody BatchDeleteCommand command) {
        log.info("批量删除等级特权, ids: {}", command.getIds());
        List<DeleteLevelPrivilegeOperateDTO> operateDTOList = levelPrivilegeService.batchDeleteLevelPrivilege(command);
        return Result.ok(operateDTOList);
    }
}
